import { RouteLocationNormalized, Router } from 'vue-router/dist/vue-router.d';
import { stringifyQuery } from './common';

/** 全局路由守卫 */
export function routerGuard(router: Router) {
    router.beforeEach(async (to, from) => {
        return toMpaRouter(to, from);
    });
}
function toMpaRouter(
    to: RouteLocationNormalized,
    from: RouteLocationNormalized,
) {
    if (to.meta.multiple === true && from.name) {
        window.open(`${to.path}?${stringifyQuery(to.query)}`, '_blank');
        return false;
    }
    return true;
}
